import os
import nirvanix
import nirvanixutils
from nirvanix import Nirvanix
from nirvanix import Session

from google.appengine.ext.webapp import template
from google.appengine.ext import webapp
import wsgiref.handlers

appkey = 'YOURAPPKEY'
username = 'YOURUSERNAME'
password = 'YOURPASSWORD'

class Download(webapp.RequestHandler):
    def get(self):
        path = self.request.get('path')

        try:
            session = Session(appkey, username, password)
        except nirvanixutils.NirvanixException, e:
            if e.code == 80001:
                self.response.out.write("Login Failed, Please correct the username and password you provided and try again.")
                return
            else:
                self.response.out.write("The following error occured while logging in: " + e.message)
        maxentries = 500
        imfs = Nirvanix('IMFS', session);
        options = {'filePath':path,'expiration':'100','restrictedIP':'false'}
        try:
            doc = imfs.GetOptimalUrls(options,False)
            self.redirect(doc.getElementsByTagName("DownloadURL")[0].childNodes[0].data);
        except nirvanixutils.NirvanixException, e:
            self.response.out.write(e)
            self.response.out.write(e.code)

def main():
    application = webapp.WSGIApplication([('/download/', Download)],debug=True)
    wsgiref.handlers.CGIHandler().run(application)

if __name__ == "__main__":
    main()
    

